/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package codegener.tool;

import codegener.entity.ColumnInfo;
import codegener.entity.TableInfo;
import java.util.ArrayList;

import java.util.List;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;


/**
 * 达梦表信息获取
 * @author 32181
 */
public class MySQLTabGet {
    
    public List<String> listTables(){
        try{
           return DBGet.getQR().query(DBGet.getConn(),"SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema=database()", new ColumnListHandler<String>());
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    public List<String> listTables(String likeName){
        try{
           return DBGet.getQR().query(DBGet.getConn(),"SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema=database() and TABLE_NAME LIKE '%"+likeName+"%'", new ColumnListHandler<String>());
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    public List<TableInfo> tableDat(String tabName){
        try{
            if(null==tabName){
                return DBGet.getQR().query(DBGet.getConn(),"SELECT TABLE_NAME name,table_rows numRows FROM INFORMATION_SCHEMA.TABLES WHERE table_schema=database() order by table_name", new BeanListHandler<TableInfo>(TableInfo.class));
            }else{
               return DBGet.getQR().query(DBGet.getConn(),"SELECT TABLE_NAME name,table_rows numRows FROM INFORMATION_SCHEMA.TABLES WHERE table_schema=database() and TABLE_NAME = '"+tabName+"' order by table_name", new BeanListHandler<TableInfo>(TableInfo.class));     
            }
           
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    public List<TableInfo> getTable(String... tableNames)throws Exception{
        if(null==tableNames||tableNames.length==0){
            return null;
        }
        List<TableInfo> rsInfos=new ArrayList<>();
        for(String tableName:tableNames){
            TableInfo tableInfo=new TableInfo();
            List<ColumnInfo> columns=DBGet.getQR().query(DBGet.getConn(),"SELECT c.`table_name` tableName,`column_name` NAME,`column_name` cname,`data_type` dataType,"
                                                       + " `character_maximum_length` maxLength,`is_nullable` isNull,"
                                                       +" `column_comment` comments,t.TABLE_COMMENT tabcomment  from information_schema.COLUMNS c LEFT JOIN information_schema.`TABLES` t ON t.TABLE_NAME=c.TABLE_NAME "
                                                       + " WHERE c.TABLE_NAME='"+tableName+"' and t.TABLE_SCHEMA NOT IN ('information_schema','mysql') and c.TABLE_SCHEMA NOT IN ('information_schema','mysql')", new BeanListHandler<ColumnInfo>(ColumnInfo.class));
            if(null!=columns&&!columns.isEmpty()){
                tableInfo.setName(tableName);
                tableInfo.setColumns(columns);
                tableInfo.setTabcomment(columns.get(0).getTabcomment());
            }

            List<String> priColumns=DBGet.getQR().query(DBGet.getConn(),"SELECT `column_name` FROM information_schema.COLUMNS WHERE column_key='pri' AND TABLE_NAME='"+tableName+"'", new ColumnListHandler<String>());

            if(null!=priColumns&&!priColumns.isEmpty()){
                for(String priColumn:priColumns){
                    for(ColumnInfo column:columns){
                        if(column.getCname().equals(priColumn)){
                            column.setIsPrimary(priColumn);
                            column.setPrimary(true);
                        }
                        if("Y".equalsIgnoreCase(column.getIsNull())){
                            column.setNullAble(true);
                        }
                    }
                }            
            }
            rsInfos.add(tableInfo);
        }
        
        return rsInfos;
    }
    
}
